home *** CD-ROM | disk | FTP | other *** search
/ FishMarket 1.0 / FishMarket v1.0.iso / fishies / 501-525 / disk_517 / cwtoy / src / consolestuff.c < prev    next >
C/C++ Source or Header  |  1992-05-06  |  4KB  |  171 lines

  1. /* consolestuff.c */
  2.  
  3. /*#include "exec/types.h"
  4. #include "intuition/intuition.h"
  5. #include "exec/memory.h"
  6. #include "functions.h"*/
  7. #include "cwtoy.h"
  8.  
  9. extern struct IOStdReq *CreateStdIO();
  10. extern struct MsgPort *CreatePort();
  11.  
  12. struct ConIOBlocks {
  13.     struct IOStdReq *writeReq;    /* I/O write request */
  14.     struct IOStdReq *readReq;    /* I/O read request */
  15.     struct MsgPort *tpr;    /* pointer to ReplyPort */
  16.                             /* for the console read */
  17. };
  18.  
  19. struct ConIOBlocks *
  20. CreateConsole(window)
  21.     struct Window *window;
  22. {
  23.     struct ConIOBlocks *c;
  24.     struct MsgPort *tpw;
  25.     int error;
  26.     
  27.     c = (struct ConIOBlocks *)AllocMem((LONG)sizeof(struct ConIOBlocks),MEMF_CLEAR);
  28.     
  29.     if (c == 0) /* out of RAM */
  30.         goto cleanup1;
  31.         
  32.     tpw = CreatePort(0,0);  /* reply port for write */
  33.     if (tpw == 0) 
  34.         goto cleanup2;
  35.     
  36.     c->tpr = CreatePort(0,0); /* reply port for read */
  37.     if (c->tpr ==0)
  38.         goto cleanup3;
  39.         
  40.     c->writeReq = CreateStdIO(tpw);
  41.     if (c->writeReq == 0)
  42.         goto cleanup4;
  43.     
  44.     c->readReq = CreateStdIO(c->tpr);
  45.     if (c->readReq == 0)
  46.         goto cleanup5;
  47.         
  48.     c->writeReq->io_Data = (APTR)window;
  49.     c->writeReq->io_Length = sizeof(struct Window);
  50.     
  51.     error = OpenDevice("console.device",0,c->writeReq,0);
  52.     if (error != 0)
  53.         goto cleanup6;    /* cannot open the console! */
  54.         
  55.     c->readReq->io_Device = c->writeReq->io_Device;
  56.     c->readReq->io_Unit   = c->writeReq->io_Unit;
  57.     /* Above copies the I/O request block from a */
  58.     /* block initialized from a successful open. */
  59.     /* Means both read and write are talking to the */
  60.     /* same instance of a console.   */
  61.     return(c);    /* pointer to the ConIOBlocks */
  62.                 /* containing both read and */
  63.                 /* write control blocks. */
  64. cleanup6:
  65.     DeleteStdIO(c->readReq);
  66. cleanup5:
  67.     DeletePort(c->tpr);
  68. cleanup4:
  69.     DeleteStdIO(c->writeReq);
  70. cleanup3:
  71.     DeletePort(tpw);
  72. cleanup2:
  73.     FreeMem(c, sizeof(struct ConIOBlocks));
  74. cleanup1:
  75.     return(NULL);
  76. }
  77.  
  78. DeleteConsole(c)
  79.     struct ConIOBlocks *c;
  80. {
  81.     struct MsgPort *mp;
  82.     AbortIO(c->readReq);    /* abort any read in progress */
  83.     CloseDevice(c->writeReq);  /*close the console device */
  84.     
  85.     mp = c->writeReq->io_Message.mn_ReplyPort;
  86.     
  87.     DeleteStdIO(c->writeReq);
  88.     DeletePort(mp);
  89.     
  90.     mp = c->readReq->io_Message.mn_ReplyPort;
  91.     
  92.     DeleteStdIO(c->readReq);
  93.     DeletePort(mp);
  94.     
  95.     FreeMem(c, sizeof(struct ConIOBlocks));
  96.     return(0);
  97. }
  98.  
  99. #define CONREAD c->readReq
  100. #define CONWRITE c->writeReq
  101.  
  102. /* ask console, asynchronously, to read a character */
  103.  
  104. EnqueueRead(c, location)
  105.     struct ConIOBlocks *c;
  106.     char *location;
  107. {
  108.     struct IOStdReq *conr;
  109.     conr = c->readReq;
  110.     
  111.     conr->io_Command = CMD_READ;
  112.     conr->io_Length =1;
  113.     conr->io_Data = (APTR) location;
  114.             /* buffer into which to store data read */
  115.     SendIO(conr);
  116.             /* asynchronous posting of a read request */
  117. }
  118.  
  119. /* write a specified number of characters from a buffer to
  120.  * a particular console device.
  121.  */
  122. WriteConsole(c, data, length)
  123.     struct ConIOBlocks *c;
  124.     char *data;
  125.     WORD length;
  126. {
  127.     struct IOStdReq *conw;
  128.     conw = c->writeReq;
  129.     
  130.     conw->io_Command = CMD_WRITE;    /* what to do */
  131.     conw->io_Length = length;        /* how many characters */
  132.     conw->io_Data = (APTR) data;    /* where it is */
  133.     DoIO(conw);        /* synchronous... wait until console */
  134.                     /* task has accepted the data  before */
  135.                     /* going on with something else.    */
  136. }
  137.  
  138. int
  139. CGetCharacter(c, wait)
  140.         /* If wait is TRUE, wait until a character             */
  141.         /* is typed.  Return the character as the result.     */
  142.         /* If wait is FALSE, return the character value        */
  143.         /* if a character is ready; otherwise return -1.    */
  144.         
  145.     struct ConIOBlocks *c;
  146.     BOOL wait;
  147. {
  148.     struct MsgPort *mp;
  149.     struct IOStdReq *conr;
  150.     char *dataAddr;
  151.     int temp;
  152.     
  153.     mp = c->tpr;    /* find the read reply port */
  154.     if(wait)
  155.     {
  156.         WaitPort(mp);
  157.     }
  158.     conr = (struct IOStdReq *) GetMsg(mp);
  159.     if(conr == 0)
  160.     {
  161.         return(-1);
  162.     }
  163.     else
  164.     {
  165.         dataAddr = (char *)conr->io_Data;
  166.         temp = *dataAddr;     /* get the value */
  167.         EnqueueRead(c, dataAddr);  /* continue the read */
  168.         return(temp);
  169.     }
  170. }    /* end of CGetChar */ 
  171.